package com.sfy.les.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.sfy.les.dictionary.DataBaseDictionary;

public class DBConnection {

	/**
	 * 
	 * @param dbInfo ,数据库连接相关信息
	 * @return
	 */
	public static Connection getConnection(DataBaseDictionary dbInfo){
		//初始化连接信息
		Connection conn = null;
		
		//获取数据库驱动信息
		String dbDriver = dbInfo.getDbDriver();
		
		//获取数据库地址
		String dbUrl = dbInfo.getDbUrl();
		
		//获取数据库登录名称
		String dbUserName = dbInfo.getDbUserName();
		
		//获取数据库登录密码
		String dbPassword = dbInfo.getDbPassword();
		//连接数据库
		try {
			Class.forName(dbDriver);
			/*Properties prop=new Properties();
			prop.setProperty("user",dbUserName);
		    prop.setProperty("password",dbPassword);
		    prop.setProperty("oracle.jdbc.V8Compatible","true");
			conn = DriverManager.getConnection(dbUrl,prop);*/
			conn = DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return conn;
	}
	
	/**
	 *  关闭数据源连接
	 * @param conn(Connection)
	 * @param st(Statement)
	 * @param rs(ResultSet)
	 */
	public static void closeConnection(Connection conn,Statement st,ResultSet rs){
		try {
			if (rs != null) {
				rs.close();
			}
			if (st != null) {
				st.close();
			}
			if (conn != null) {
				conn.close();
			}
		}catch(Throwable t){
			t.printStackTrace();
		}
	}
}
